import glob
import os
import sys
import time

# ==============================================================================
# -- Find CARLA module ---------------------------------------------------------
# ==============================================================================

try:
    sys.path.append(
        glob.glob(
            '../carla/dist/carla-*%d.%d-%s.egg'
            % (
                sys.version_info.major,
                sys.version_info.minor,
                'win-amd64' if os.name == 'nt' else 'linux-x86_64',
            )
        )[0]
    )
except IndexError:
    pass

# ==============================================================================
# -- Add PythonAPI for release mode --------------------------------------------
# ==============================================================================
try:
    sys.path.append(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/carla'
    )
    sys.path.append("../examples/")
except IndexError:
    pass

import carla

# Connect to the client and retrieve the world object
try:
    client = carla.Client('127.0.0.1', 2000)
    world = client.get_world()
    actors = world.get_actors()
    vehicle_actor_list = actors.filter('*vehicle*')
    vehicle_actor = vehicle_actor_list[0]
    # vehicle_location = vehicle_actor.get_location()

    while True:
        # actor=world.get_actor( 623)
        location = vehicle_actor.get_location()
        transform = vehicle_actor.get_transform()
        x = transform.location.x
        y = transform.location.y
        z = transform.location.z
        # vehicle_blueprints = world.get_blueprint_library().filter('**')
        # print(vehicle_blueprints)

        print(f"x:{x},y:{y},z:{z}")
        print("running")

        # print(actor)
        # print(actor.type_id)
        # print(transform)
        time.sleep(0.1)
    # world.tick()

    # ego_vehicle_actor=actors.find(263)
    # location=ego_vehicle_actor.get_location()
    # transform=ego_vehicle_actor.get_transform()
    # print(location)
    # print(transform)
    # client.load_world('Town01')
    # client.set_timeout(20.0)
    # for i in range(len(client.get_available_maps())):
    #     print(client.get_available_maps()[i])
except IndexError:
    pass
